package cn.chinaflame.pagodas.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import cn.chinaflame.pagodas.Result.Result;
import cn.chinaflame.pagodas.service.PaBadLightsService;
import cn.chinaflame.pagodas.service.PaEmailLogService;
import cn.chinaflame.pagodas.service.PaProductPointService;
import cn.chinaflame.pagodas.service.QuartzService;
import cn.chinaflame.pagodas.service.WrmNIService;
import cn.chinaflame.pagodas.utils.email.Email;
import cn.chinaflame.pagodas.utils.email.EmailService;
import cn.chinaflame.pagodas.vo.LightVO;

/**
 * 定时任务配置接口
 *
 * @author boming
 * @date 2018年6月22日 上午9:55:42
 */
@RestController
@RequestMapping(value = "/openapi/v1/pagodas/quartz/")
public class QuartzController {

    private static final Logger log = LoggerFactory.getLogger(QuartzController.class);
    
    @Autowired
    private WrmNIService wrmNIService;
    
    @Autowired
    private PaBadLightsService paBadLightsService;
    
	@Autowired
	private PaProductPointService paProductPointService;
	@Autowired
    private Email email;
	
	@Autowired
	private PaEmailLogService paEmailLogService;
	
    @Autowired
    private EmailService emailService;
  
    /**
     * 订单状态定时任务（时间间隔配置）
     *
     * @param interval 时间间隔，单位分钟
     * @return
     */
    @GetMapping(value = "order/interval")
    public Object orderInterval(long interval) {
        log.info("订单状态定时任务间隔时间参数配置，默认1分钟【单位分】interval:{}", interval);
        QuartzService.orderInterval = interval;
        return Result.create("配置成功");
    }

    /**
     * 点灯/熄灯操作定时任务（时间间隔配置）
     *
     * @param interval 时间间隔，单位500毫秒
     * @return；
     */
    @GetMapping(value = "productPoint/interval")
    public Object productPointInterval(long interval) {
        log.info("点灯/熄灯操作定时任务间隔时间参数配置，默认500毫秒【单位500毫秒】interval:{}", interval);
        QuartzService.productPointInterval = interval;
        return Result.create("配置成功");
    }
    
    /**
     * 定时寻灯接口
     * @return
     */
    @GetMapping(value = "productPoint/search")
    public Object scheduledLightSearch(String lightnos) {
    	log.info("lightnos={}",lightnos);
    	List<LightVO> lights = wrmNIService.lightSearch(lightnos);
    	int[] result = paBadLightsService.batchAddBadLight(lights);
    	log.info("result={}",result);
    	return result;
    }
    /**
     * 定时寻灯接口
     * @return
     */
    @GetMapping(value = "productPoint/search1")
    public Object scheduledLightSearch1(String areano) {
    	log.info("areano={}",areano);
    	List<String> lights = paProductPointService.genLightNos(areano);
    	String lightnos =  String.join(",",lights);
    	wrmNIService.lighton();
    	List<LightVO> lightvos = wrmNIService.lightSearch(lightnos);
    	List<Integer> result = Lists.newArrayList();
    	for(LightVO vo:lightvos) {
    		if(vo.getResult() == 0) {
    			int n = paBadLightsService.addBadLight(vo);
    			result.add(n);
    		}
    	}
    	log.info("result={}",result);
    	return result;
    }
    
}
